package com.whut.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whut.customer.dao.SysUserMapper;
import com.whut.customer.entity.SysUser;
import com.whut.customer.service.ISysUserService;
import com.whut.customer.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author author
 * @since 2021-01-22
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Resource
    private ISysUserService userService;

    @Override
    public SysUser getUserByUserName(String username) {
        return baseMapper.getUserByUserName(username);
    }

    @Override
    public boolean addUser(SysUser user) {
        // 随机生成salt
        String saltDB = randGenerateSalt(6);
        String realSalt = saltDB + user.getUsername();
        user.setSalt(saltDB);
        // 密码加密
        String passDB = MD5Util.getDbPassByShiro(user.getPassword(),realSalt);
        user.setPassword(passDB);
        baseMapper.insert(user);
        return true;
    }

    @Override
    public List<SysUser> selectUser(SysUser user) {
        user.setDelFlag(0);
        return baseMapper.selectList(queryWrapper(user));
    }

    @Override
    public boolean updateUser(SysUser user) {
        if(user.getPassword()!=null){
            // 随机生成盐
            String saltDB = randGenerateSalt(6);
            String realSalt = saltDB + user.getUsername();
            user.setSalt(saltDB);
            String passDB = MD5Util.getDbPassByShiro(user.getPassword(),realSalt);
            user.setPassword(passDB);
        }
        return userService.saveOrUpdate(user);
    }

    // 随机生成盐 参数length为长度
    private String randGenerateSalt(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<length; i++){
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    QueryWrapper<SysUser> queryWrapper(SysUser user){
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(user.getId()!=null,SysUser::getId,user.getId());
        queryWrapper.lambda().eq(user.getUsername()!=null,SysUser::getUsername,user.getUsername());
        queryWrapper.lambda().like(user.getRealName()!=null,SysUser::getRealName,user.getRealName());
        queryWrapper.lambda().eq(user.getMobile()!=null,SysUser::getMobile,user.getMobile());
        queryWrapper.lambda().eq(user.getEmail()!=null,SysUser::getEmail,user.getEmail());
        queryWrapper.lambda().eq(user.getWx()!=null,SysUser::getWx,user.getWx());
        queryWrapper.lambda().eq(user.getGender()!=null,SysUser::getGender,user.getGender());
        queryWrapper.lambda().eq(user.getDelFlag()!=null,SysUser::getDelFlag,user.getDelFlag());
        return queryWrapper;
    }
}
